package com.forum.controller;

import com.forum.common.api.CommonResult;
import com.forum.model.dto.PostDTO;
import com.forum.model.vo.CategoryVO;
import com.forum.model.vo.PostVO;
import com.forum.model.vo.TagVO;
import com.forum.service.PostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@Tag(name = "帖子管理接口")
@RestController
@RequiredArgsConstructor
public class PostController {

    private final PostService postService;
    private static final Logger log = LoggerFactory.getLogger(PostController.class);

    @Operation(summary = "创建帖子")
    @PostMapping("/post/create")
    public CommonResult<PostVO> createPost(@Validated @RequestBody PostDTO postDTO) {
        return CommonResult.success(postService.createPost(postDTO));
    }

    @Operation(summary = "获取帖子详情")
    @GetMapping("/post/{id}")
    public CommonResult<PostVO> getPost(@PathVariable Long id) {
        postService.incrementViews(id);
        return CommonResult.success(postService.getPost(id));
    }

    @Operation(summary = "更新帖子")
    @PutMapping("/post/{id}")
    public CommonResult<PostVO> updatePost(@PathVariable Long id, @Validated @RequestBody PostDTO postDTO) {
        return CommonResult.success(postService.updatePost(id, postDTO));
    }

    @Operation(summary = "删除帖子")
    @DeleteMapping("/post/{id}")
    public CommonResult<Void> deletePost(@PathVariable Long id) {
        postService.deletePost(id);
        return CommonResult.success(null);
    }

    @Operation(summary = "上传图片")
    @PostMapping("/post/upload/image")
    public CommonResult<String> uploadImage(@RequestParam("file") MultipartFile file) {
        log.info("接收到上传图片请求, 文件名: {}, 大小: {} bytes, 类型: {}", 
            file.getOriginalFilename(), file.getSize(), file.getContentType());
        try {
            String imageUrl = postService.uploadImage(file);
            log.info("图片上传成功, 返回URL: {}", imageUrl);
            return CommonResult.success(imageUrl);
        } catch (Exception e) {
            log.error("图片上传失败: {}", e.getMessage(), e);
            return CommonResult.fail(e.getMessage());
        }
    }

    @Operation(summary = "获取分类列表")
    @GetMapping("/categories")
    public CommonResult<List<CategoryVO>> getCategories() {
        return CommonResult.success(postService.getCategories());
    }

    @Operation(summary = "获取标签列表")
    @GetMapping("/tags")
    public CommonResult<List<TagVO>> getTags() {
        return CommonResult.success(postService.getTags());
    }

    @Operation(summary = "获取用户帖子列表")
    @GetMapping("/post/user/{userId}")
    public CommonResult<List<PostVO>> getUserPosts(@PathVariable Long userId) {
        return CommonResult.success(postService.getUserPosts(userId));
    }

    @Operation(summary = "获取分类帖子列表")
    @GetMapping("/post/category/{categoryId}")
    public CommonResult<List<PostVO>> getCategoryPosts(@PathVariable Long categoryId) {
        return CommonResult.success(postService.getCategoryPosts(categoryId));
    }

    @Operation(summary = "获取标签帖子列表")
    @GetMapping("/post/tag/{tagId}")
    public CommonResult<List<PostVO>> getTagPosts(@PathVariable Long tagId) {
        return CommonResult.success(postService.getTagPosts(tagId));
    }
} 